﻿#include "FormTileImage.h"
#include "ui_FormTileImage.h"
#include <QFileDialog>
#include "ComputeLevels.h"
#include "TileService.h"
#include "pakservice.h"

FormTileImage::FormTileImage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormTileImage)
{
    ui->setupUi(this);
    connect(ui->pushButtonAddFile,SIGNAL(clicked(bool)),this,SLOT(slotAddFile()));
    connect(ui->pushButtonGetLV,SIGNAL(clicked(bool)),this,SLOT(slotGetLevels()));
    connect(ui->pushButtonOK,SIGNAL(clicked(bool)),this,SLOT(slotOK()));
    connect(ui->pushButtonCancel,SIGNAL(clicked(bool)),this,SLOT(slotCancel()));
    connect(ui->pushButtonSltOutPath,SIGNAL(clicked(bool)),this,SLOT(slotSltOutPath()));
    connect(ui->pushButtonClearFiles,SIGNAL(clicked(bool)),this,SLOT(slotClearFiles()));

    ui->tableWidget->setColumnWidth(0,ui->tableWidget->width());
}

FormTileImage::~FormTileImage()
{
    delete ui;
}

TileOption FormTileImage::_getTileOption()
{
    TileOption option;
    option.tilesize = ui->lineEditTileSize->text().toInt();
    option.tile_lon_0= option.extent.width() / (ui->lineEditlv1x->text().toInt());
    option.tile_lat_0= option.extent.height() / (ui->lineEditlv1y->text().toInt());
    return option;
}

QString FormTileImage::_getInputFile(int i)
{
    if(ui->tableWidget->rowCount() <= i) return "";
    return ui->tableWidget->item(i, 0)->text();
}

void FormTileImage::slotAddFile()
{
    QString str = QFileDialog::getOpenFileName(nullptr, QStringLiteral("选择"));
    if (str.length() <= 0) {
        return;
    }

    int rowcount = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(rowcount+1);
    QTableWidgetItem *newItem = new QTableWidgetItem(str);
    ui->tableWidget->setItem(rowcount, 0, newItem);
}

void FormTileImage::slotClearFiles()
{
    ui->tableWidget->setRowCount(0);
}

void FormTileImage::slotGetLevels()
{
    QString path =_getInputFile(0);
    if(path.length() <= 0) return;
    TileOption op = this->_getTileOption();



    ComputeLevels cl;
    int min=0,max=0;
    cl.computeLevels(&op,path,min,max);
    ui->lineEditMinLV->setText(QString::number(min));
    ui->lineEdit_MaxLV->setText(QString::number(max));
}

void FormTileImage::slotOK()
{

    if(ui->comboBoxType->currentText()=="TILE")
    {
        QString str = ui->tableWidget->item(0,0)->text();
        if(str.length() <= 1) return;
        TileServiceOption option;
        option.tileOption = _getTileOption();
        option.taskType = TileServiceOption::IMG;
        option.tileType = TileServiceOption::IMG_DEF;
        option.inputFiles.append(str);
        option.level0= ui->lineEditMinLV->text().toInt();
        option.level1= ui->lineEdit_MaxLV->text().toInt();
        if(option.level0 > option.level1) return;

        option.outDir = ui->lineEditOutPath->text();
        if(option.outDir.length() <= 1)return;

        if(ui->comboBoxFileType->currentText() == "PNG"){
            option.ext = ".png";
        }
        else if(ui->comboBoxFileType->currentText() == "PNG"){
            option.ext = ".jpg";
        }else{
            return;
        }
        if(ui->comboBoxCoord->currentText() =="WGS84")
        {
             option.latlan = "WGS84";
        }else if(ui->comboBoxCoord->currentText() =="WebMercator")
        {
             option.latlan = "WebMercator";
        }


        if(_tileService ==0){
            _tileService = new TileService();
        }
        if(_tileService->isRunning()) return;
        _tileService->handle(option);

    }else
    {
        QString str = ui->tableWidget->item(0,0)->text();
        if(str.length() <= 1) return;
        pakServiceOption option;
        option.tileOption = _getTileOption();
        option.inputFiles.append(str);
        option.level0= ui->lineEditMinLV->text().toInt();
        option.level1= ui->lineEdit_MaxLV->text().toInt();
        if(option.level0 > option.level1) return;

        option.outDir = ui->lineEditOutPath->text();
        if(option.outDir.length() <= 1)return;

        if(ui->comboBoxFileType->currentText() == "PNG"){
            option.ext = "png";
        }
        if(ui->comboBoxFileType->currentText() == "JPEG"){
            option.ext = "jpg";
        }

        if(ui->comboBoxCoord->currentText() =="WGS84")
        {
             option.latlan = "WGS84";
        }else if(ui->comboBoxCoord->currentText() =="WebMercator")
        {
             option.latlan = "WebMercator";
        }


        if(_pakService ==0){
            _pakService = new pakService();
        }
        if(_pakService->isRunning()) return;
        _pakService->handle(option);

    }

}

void FormTileImage::slotCancel()
{
    if(_tileService){
        _tileService->terminate();
    }
}

void FormTileImage::slotSltOutPath()
{
    QString file_dir = QFileDialog::getExistingDirectory(this,
                   "选择保存路径",
                   "./",
                    QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    if( file_dir.isEmpty() )
    {
        return;
    }
    else
    {
        ui->lineEditOutPath->setText( file_dir );
    }
}
